package pl.ptemich.bumbastic;


import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.ImageBuffer;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Start extends BasicGame {

	private static final Logger LOGGER = LoggerFactory.getLogger(Start.class); 
	
    private Sprite wizard; // = new Sprite(new Image("resources/images/cursor.png"));
    
    private ImageBuffer mandel;
    
    private Image mandelImage;
    
    private int repeatations = 15;
    
    private int delatPeriod = 1000;
    
    //private static final int WIDTH = 800;
    
    //private static final int HEIGHT = 600;
    
    //private static final int BAR_HEIGHT = 50;
    
    //private Image background; 
    
    //private Image plane;
    
    //private Image shoot;
    
    //private Music backgroundMusic;
    
    //private float x = 350;
    
    //private float y = 300;
    
   // private List<Animation> explosions;
    
    //private float explosionBreak = 0;
    
    
    //private Sound explosionSound;
    
    //private SpriteSheet explosionSprites; 
    
    //private Animation explosionAnimation;
    
    //private Hex hex;
    
    //private Board board;
    
    public Start() {
        super("przemo rulez");        
    }
    
    
    private boolean check(Complex complex) {                
        Complex Zn = complex;
        Complex C = new Complex(0, 1);
        double abs;//
        //int enh = 2;
        
        for (int i = 0; i < repeatations; i++) {
            abs = Zn.abs(); 
            if (abs > 2) {
                return false;
            }
            Zn = Zn.doubled().add(C);
        }
        return true;
    }

    
    public void fract() {          
        double xBase = -1.5f;
        double yBase = -1.5f;
        
        double xEnd = 1.5f;
        double yEnd = 1.5f;
        
        double scale = 0.01f;
        
        int yIterations = Double.valueOf((yEnd - yBase) / scale).intValue();
        int xIterations = Double.valueOf((xEnd - xBase) / scale).intValue();
        
        
        
        for (int y = 0; y < yIterations; y++) {
            for (int x = 0; x < xIterations; x++) {
                if (check(new Complex(xBase + scale * x, yBase + scale * y))) {
                    mandel.setRGBA(x, y, 255, 255, 255, 255);
                } else {
                    mandel.setRGBA(x, y, 0, 0, 0, 255);
                }
            }
        }        
        
        Image tmp = mandelImage;
        mandelImage = mandel.getImage();
        try {
            tmp.destroy();
        } catch (SlickException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    
    @Override
    public void init(GameContainer container) throws SlickException {

        
        mandel = new ImageBuffer(400, 400);
        mandelImage = mandel.getImage();
        
        wizard = new Sprite(new Image("resources/images/wizard.png"), 100, 200);
        fract();

        
        //container.setMouseCursor(new Image("resources/images/cursor.png"), 2, 2);
        
        //background = new Image("resources/images/map.png");
//        plane = new Image("resources/images/plane.png");
//        plane.setCenterOfRotation(plane.getWidth() / 2, plane.getHeight() /2);
//        shoot = new Image("resources/images/shoot.png");
//        shoot.setCenterOfRotation(shoot.getWidth() / 2, shoot.getHeight() /2);
        
        //explosions = new ArrayList<Animation>();
        //Shoot newShoot = new Shoot();
        //newShoot.setPosX(50).setPosY(100);
        //shoots.add(newShoot);
        
//        backgroundMusic = new Music("resources/music/background.ogg");
//        backgroundMusic.loop();
        
        //shootSound = new Sound("resources/sounds/machine.ogg");
        //explosionSound = new Sound("resources/sounds/explosion.ogg");
        
        //explosionSprites = new SpriteSheet(new Image("resources/images/explosion.png"), 36, 50);
        
        //System.out.println(explosionSprites.getHorizontalCount() + ":" + explosionSprites.getVerticalCount());
        
        //explosionAnimation = new Animation(explosionSprites, 70);
        
        //hex = new Hex(20, 100, 100);
        //board = new Board(10, 10);
    }
    
    @Override
    public void render(GameContainer container, Graphics graphics) throws SlickException {        
    	//background.draw(0, 0, WIDTH, HEIGHT); 
        
        //board.draw(graphics);
        
        //hex.draw(graphics);
//        
//        for (Shoot shootObj : shoots) {
//            shoot.setRotation(shootObj.rotation);
//            shoot.draw(shootObj.posX, shootObj.posY, 1.0f);
//        }
        
        //explosionAnimation.draw(100, 100);
        //plane.draw(x, y, 1.0f);
        
        wizard.draw(graphics);
        //mandelImage.draw(10, 10);
    }
    
    @Override
    public void update(GameContainer container, int delta) throws SlickException {
        Input input = container.getInput();

        
        if (input.isKeyDown(Input.KEY_ESCAPE)) {
            container.exit();
        }
        
        if (input.isControllerRight(0)) {
            wizard.moveRight();
        } else if (input.isControllerLeft(0)) {
            wizard.moveLeft();
        } else {
            wizard.stop();
        }
        
        if (input.isButton1Pressed(0)) {
            wizard.jump();
        }         
        
        if (input.isKeyDown(Input.KEY_UP)) { 
            wizard.jump();  
        }        
        
        if (input.isKeyDown(Input.KEY_RIGHT)) {
            wizard.moveRight();
        } else if (input.isKeyDown(Input.KEY_LEFT)) {
            wizard.moveLeft();
        } else {
            wizard.stop();
        }
        
        wizard.update(delta);
        
//        delatPeriod -= delta;
//        if (delatPeriod < 0) {
//            delatPeriod = 100;
//            repeatations++;            
//            if (repeatations >20) {
//                repeatations = 1; 
//            }
//            fract();
//        }
        
//        if(input.isKeyDown(Input.KEY_LEFT))
//        {
//            plane.rotate(-0.2f * delta);
//        }
// 
//        if(input.isKeyDown(Input.KEY_RIGHT))
//        {
//            plane.rotate(0.2f * delta);
//        }
// 
//        float rotation = plane.getRotation();
//        
//        if(input.isKeyDown(Input.KEY_UP))
//        {
//            float hip = 0.4f * delta;
//             
// 
//            x+= hip * Math.sin(Math.toRadians(rotation));
//            
//            x = (x > WIDTH - BAR_HEIGHT) ? WIDTH - BAR_HEIGHT : x;
//            x = (x < 0) ? 0 : x;
//            
//            y-= hip * Math.cos(Math.toRadians(rotation));
//            
//            y = (y > (HEIGHT - BAR_HEIGHT)) ? (HEIGHT - BAR_HEIGHT) : y;
//            y = (y < 0) ? 0 : y;
//        }
 
        //shootBreak -= delta;
        
//        if (input.isKeyDown(Input.KEY_SPACE) && shootBreak < 0) {
//            shoots.add(new Shoot().setPosX(x + plane.getWidth() / 2 - shoot.getWidth() / 2)
//                                  .setPosY(y + plane.getHeight() / 2)
//                                  .setSpeedX(Math.sin(Math.toRadians(rotation)))
//                                  .setSpeedY(-Math.cos(Math.toRadians(rotation)))
//                                  .setRotation(rotation));
//            shootBreak = 200;
//           
//        }
        
        //explosionBreak -= delta;
        
//        if (input.isKeyDown(Input.KEY_SPACE) && explosionBreak < 0) {
//            explosionAnimation.draw(50, 50);
//            explosionSound.playAt(x, y, 1);
//            explosionBreak = 200;
//            
//            
//        }
        
//        for (Shoot shootObj : shoots) {
//            shootObj.update(delta);            
//        }

        //if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
           // board.mouseClick(input.getMouseX(), input.getMouseY());
        //}
    } 
    
    
    public static void main(String[] args) throws SlickException {
        AppGameContainer app = new AppGameContainer(new Start());
        app.setDisplayMode(800, 600, false);
        app.setShowFPS(true);
        //app.setTargetFrameRate(75);
        app.setVSync(true);
        app.setFullscreen(true);
        app.start();        
        
    }

}
